<?php 

class Application_Model_Category
{
	protected $_dbTable;
	
	public function __construct() {
		$this->_dbTable = new Application_Model_DbTable_Config();
	}
	
	public function addConfig ($data) 
	{
		$this->_dbTable->insert($data);
	}
	
	public function updateConfig($data, $id) 
	{
		$this->_dbTable->update($data, $this->_primary."={$id}");
	}
	
	public function fetchAllOrderDesc() 
	{
		$select = $this->_dbTable->select()
        ->order('cat_id desc');
		return $this->_dbTable->fetchAll($select)->toArray();
	}
	
	public function getById($cat_id)
	{
		$rows = $this->_dbTable->fetchAll(
			$this->_dbTable->select()
				->where('cat_id = ?', $cat_id)
				->limit(1, 0)
			);
		return $rows[0];
	}
	
	public function getLatestConfig($num_post = 10) 
	{
		$rows = $this->_dbTable->fetchAll(
			$this->_dbTable->select()
				->order('cat_id desc')
				->limit($num_post, 0)
			);
		return $rows;
	}
	public function filter($resource)
	{
		$cates= array();
		
		if($resource) foreach ($resource as $c) {
			$cates [$c['cat_id']] =$c['cat_title'];
		}
		return $cates;
	}
 }